package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.PageBean;
import com.cw.data.Result;
import com.cw.pojo.TbBrand;
import com.cw.service.BrandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("brandController")
public class BrandController {

    @Reference
    private BrandService brandService;

    @RequestMapping("toLogin")
    public String toLogin() {

        return "login";
    }

    @RequestMapping("toHome")
    public String toHome() {

        return "admin/home";
    }

    @RequestMapping("toIndex")
    public String toIndex() {

        return "admin/index";
    }

    @RequestMapping("brand")
    public String brand() {

        return "admin/brand";
    }

    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand tbBrand) {

        try {

            brandService.addBrand(tbBrand);

            return Result.result(ConstantEnum.SUCCESS);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getBrandInfo")
    @ResponseBody
    public Result getBrandInfo(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5")Integer pageSize) {

        try {

            PageBean<TbBrand> pa = brandService.getBrandInfo(pageNumber, pageSize);

            return Result.result(ConstantEnum.SUCCESS).setData(pa);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }

    }

    @RequestMapping("getOneBrandInfo")
    @ResponseBody
    public Result getOneBrandInfo(Long id) {

        try {

            TbBrand tb = brandService.getOneBrandInfo(id);

            return Result.result(ConstantEnum.SUCCESS).setData(tb);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updateBrand(TbBrand tbBrand) {

        try {

            brandService.updateBrand(tbBrand);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }

    }

    @RequestMapping("deleteBrand")
    @ResponseBody
    public Result deleteBrand(Long[] id) {

        try {

            brandService.deleteBrand(id);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

}
